package cn.zzp.basic.controller;

import cn.zzp.basic.exception.BusinessException;
import cn.zzp.basic.service.VerifyCodeService;
import cn.zzp.basic.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Administrator
 * @description: 验证码
 * @date 2022/7/27 19:01
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private VerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public JsonResult verifyCode(@PathVariable("key") String key) {

        try {
            String code = verifyCodeService.getVerifyCode(key);
            return JsonResult.me().setObj(code);

        } catch (BusinessException e) {
            return JsonResult.me("获取失败" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me("网络繁忙，请稍后再试");
        }

    }

    /*
        phone: this.phoneUserForm.phone,
        imageCode: this.phoneUserForm.imageCode,
        imageCodeKey: localStorage.getItem("phoneRegisterCode")
     */
    @PostMapping("/smsCode")
    public JsonResult smsCode(@RequestBody Map<String, Object> map){
        try {
            verifyCodeService.smsCode(map);
            return JsonResult.me();
        }catch (BusinessException e) {
            throw new BusinessException("发送信息失败," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody Map<String, String> map){
        try {
            verifyCodeService.binderSmsCode(map);
            return JsonResult.me();
        }catch (BusinessException e) {
            throw new BusinessException("发送信息失败," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

}
